<?php
require_once ("../plugins/Zend/Loader.php");
class request {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty,$page='',$cat='',$product='') {
		$this->db = $db;
		$this->smarty = $smarty;
		$this->page = $page;
		$this->cat = $cat;
		$this->product = $product;
	}
	
	public function main() {
		if ($_POST['act'] == 'sendrequest'){
			$this->SendRequest();
		}else if($_POST['act'] == 'sendregister'){
			$this->pre_register();
		}
		
		
		if($this->cat == 'download'){
			$this->Download();
		}else if($this->cat == 'order'){
			$countryBean = new FIN_CountryBean($db, $smarty);
			$allcountry = $countryBean->get_all_country($this->db);
			$this->smarty->assign('ALLCOUNTRY',$allcountry);
			$this->smarty->display("order.tpl");
		}else if($this->cat == 'pre-register'){
			
			$this->smarty->display("pre-register.tpl");	
		}else
			$this->smarty->display("request.tpl");	
	}
	
	
	public function Download() {
		
		//require_once 'Zend/Gdata/Docs.php';
		Zend_Loader::loadClass('Zend_Gdata');
		Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
		Zend_Loader::loadClass('Zend_Gdata_Docs');
		Zend_Loader::loadClass('Zend_Http_Client');	
	
		
		
		
		$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
		$client = Zend_Gdata_ClientLogin::getHttpClient(GUSER, GPASS, $service);
		$gdClient = new Zend_Gdata_Docs($client);
	    $feed = $gdClient->getDocumentListFeed('https://docs.google.com/feeds/default/private/full?v=3');
		$listdocs = array();
		$dem = 0;
		foreach ($feed->entries as $entry) {  
				// Find the URL of the HTML view of the document.
				$alternateLink = '';
				foreach ($entry->link as $link) {
					if ($link->getRel() === 'alternate') {
						$alternateLink = $link->getHref();
					}
				}
				$listdocs[$dem]['link']=$alternateLink;
				$listdocs[$dem]['name']=$entry->title;
				// Make the title link to the document on docs.google.com.
				//echo "<li><a href=\"$alternateLink\">\n";
			
			//echo "$entry->title\n";
			$dem++;
		  }
		  $this->smarty->assign('LISTDOCS',$listdocs);
		$this->smarty->display("download.tpl");
	}
	
	public function SendRequest() {
		$request = new FIN_requestBean($db, $smarty);
		$msg = $this->validate();	
		if($msg == ""){
			$requestback = $request->saveRequest($this->db,$_POST);
			$type = array('ID' => 0,'message' => "Your request send.");
		}else{
			$type = array('ID' => -1, 
						  'message' => $msg);	
		}
		echo array2json($type);	
	}
	
	
	public function pre_register() {
		if ($_POST['act'] == 'sendregister'){
			$msg = $this->validate();	
			if($msg == ""){
				$from = $_POST['email'];
				$to = GUSER;
				$subject = "Contact Email";
				$message = "<b>Name:</b>  " . $_POST['name'] .  "<br />" .
							"<b>Date of birth:</b>  " . $_POST['dob'] .  "<br />" .
						   "<b>E-mail:</b>  " . $_POST['email'] . "<br />" .
						   "<b>Information:</b>  " . $_POST['information'];
				$header = "Content-type: text/html\r\nFrom: $from\r\nReply-to: $from";
				
				if (mail($to, $subject, $message, $header)){
					$type = array('ID' => 0,'message' => "Your requirement send.");
				} 
				else 
				{
						$type = array('ID' => -1, 
									  'message' => "Error occurred while sending email. Please try again.");
					
				}
	
			}
			else
			{
				$type = array('ID' => -1, 
							  'message' => $msg);	
			}
			echo array2json($type);	
		}
	}
	
	
	public function check_email_address($email) 
	{
    	// First, we check that there's one @ symbol, and that the lengths are right
    	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
    	{
    	// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    	return false;
    	}
    	// Split it into sections to make life easier
    	$email_array = explode("@", $email);
    	$local_array = explode(".", $email_array[0]);
   		for ($i = 0; $i < sizeof($local_array); $i++) 
   		{
   			if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) 
   			{
   			return false;
   			}
   		}
   		if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))  // Check if domain is IP. If not, it should be valid domain name
   		{
   			$domain_array = explode(".", $email_array[1]);
   			if (sizeof($domain_array) < 2) 
   			{
   			return false; // Not enough parts to domain
   			}
   			for ($i = 0; $i < sizeof($domain_array); $i++) 
   			{
   				if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
   				{
   				return false;
   				}
   			}
   		}
   		return true;
	}
	
	
	public function validate(){
		$msg = '';
		if($_POST['name'] == "" || $_POST['name'] == 'Name')
		{
				$msg .= "Name field could not be empty .";
		}
		
		if($_POST['dob'] == "" || $_POST['dob'] == 'date of birth')
		{
				if($msg != "") $msg .= "<br />";
				$msg .= "Date of birth field could not be empty .";
		}
		
				
		if($_POST['information'] == "" || $_POST['information'] == 'information')
		{
				if($msg != "") $msg .= "<br />";
				$msg .= "Information field could not be empty .";
			
		}
		
		
		
		return $msg;
	}
	
	
	public function validate_pre_register(){
		if($_POST['name'] == "")
		{
				$msg = "Name field could not be empty .";
		}
		
		if($_POST['email'] == "")
		{
			
				if($msg != "") $msg .= "<br />";
				$msg .= "Email address field could not be empty .";
			
		}
		if($_POST['email'] != "" && $this->check_email_address($_POST['email']) == false)
		{
			
				if($msg != "") $msg .= "<br />";
				$msg .= "Invalid email address .";
			
		}
		if(isset($_POST['phone']) && $_POST['phone'] != "" && (strlen($_POST['phone']) < 6 || (!preg_match('/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/', $_POST['phone'])))){
			
				if($msg != "") $msg .= "<br />";
				$msg .= "Invalid telephone number.";
			
		}
		if($_POST['information'] == "")
		{
			
				if($msg != "") $msg .= "<br />";
				$msg .= "Information field could not be empty .";
			
		}
		
		return $msg;
	}
	
}
?>
